<template>
  <ContentWrap>
    <el-descriptions title="Customer Information" column="2" border>
      <el-descriptions-item :label="t('su.supplier.name')" align="left">{{ customer.name }}</el-descriptions-item>
      <el-descriptions-item :label="t('su.supplier.address')" align="left">{{ customer.address }}</el-descriptions-item>
      <el-descriptions-item :label="t('su.supplier.contract')" align="left">{{ customer.contract }}</el-descriptions-item>
      <el-descriptions-item :label="t('su.supplier.mobile')" align="left">{{ customer.mobile }}</el-descriptions-item>
    </el-descriptions>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<!--      <el-table-column label=" 编号" align="center" prop="id" />-->
      <el-table-column :label="t('su.invoice.no')" align="center" prop="no" fixed="left" width="200px">
        <template #default="scope">
          <router-link :to="'/su/pi/view/' + scope.row.id">
            <span link type="success">{{ scope.row.no }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column :label="t('su.invoice.status')" align="center" prop="status" fixed="left" width="80px"/>
      <el-table-column :label="t('su.invoice.customer')" align="center" prop="customerId" width="120px">
        <template #default="scope">
          <SelectTag pro-key="id" pro-name="name" :list-value="customerList" :value="scope.row.customerId" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.invoice.profit') + '$'" align="center" prop="totalProfitUs" width="120px"/>
      <el-table-column :label="t('su.invoice.totalSale') + '$'" align="center" prop="totalSaleUs" width="120px"/>
      <el-table-column :label="t('su.invoice.totalPurhase') + '$'" align="center" prop="totalPurhaseUs" width="120px"/>
      <el-table-column
        :label="t('su.invoice.invoiceDate')"
        align="center"
        prop="invoiceDate"
        :formatter="dateFormatter2"
        width="120px"
      />
      <el-table-column
        :label="t('su.invoice.dueDate')"
        align="center"
        prop="dueDate"
        :formatter="dateFormatter2"
        width="120px"
      />
      <el-table-column :label="t('su.invoice.kind')" align="center" prop="kind" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_INVOICE_KIND" :value="scope.row.kind" />
        </template>
      </el-table-column>

      <el-table-column :label="t('su.invoice.brand')" align="center" prop="brandId" width="120px">
        <template #default="scope">
          <SelectTag pro-key="id" pro-name="name" :list-value="brandList" :value="scope.row.brandId" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.invoice.buyer')" align="center" prop="buyer" />
      <el-table-column :label="t('su.invoice.po')" align="center" prop="po" />
      <el-table-column :label="t('su.invoice.payAction')" align="center" prop="payAction" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SU_INVOICD_PAY_ACTION" :value="scope.row.payAction" />
        </template>
      </el-table-column>
      <el-table-column :label="t('su.invoice.saler')" align="center" prop="term" />
<!--        <template #default="scope">-->
<!--          <dict-tag :type="DICT_TYPE.SU_INVOICE_TERM" :value="scope.row.term" />-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column :label="t('su.invoice.bill')" align="center" prop="bill" />
      <el-table-column :label="t('su.invoice.remarks')" align="center" prop="remarks" />
      <el-table-column :label="t('su.invoice.count')" align="center" prop="count" />
      <el-table-column
        :label="t('table.createTime')"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column :label="t('su.invoice.no')" align="center" min-width="300px" fixed="right">
        <template #default="scope">
          <router-link :to="'/su/pi/view/' + scope.row.id">
            <el-button link type="success">{{ t('action.preview') }}</el-button>
          </router-link>
          <el-button
            link
            type="primary"
            v-if="'Inquiry' == scope.row.status"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['su:proforma-invoice:update']"
          >
            {{ t('action.edit') }}
          </el-button>
          <el-button
            link
            type="warning"
            v-if="'Inquiry' == scope.row.status"
            @click="handleConfirm(scope.row.id)"
            v-hasPermi="['su:proforma-invoice:confirm']"
          >
            {{ t('common.confirm') }}
          </el-button>
          <el-button
            link
            type="danger"
            v-if="'Inquiry' == scope.row.status"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['su:proforma-invoice:delete']"
          >
            {{ t('action.del') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProformaInvoiceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import download from '@/utils/download'
import { ProformaInvoiceApi, ProformaInvoiceVO } from '@/api/su/pi'
import ProformaInvoiceForm from './ProformaInvoiceForm.vue'
import {CommonApi} from "@/api/su/common";

/** 形式发票 列表 */
defineOptions({ name: 'ProformaInvoice' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const route = useRoute() // 路由

const loading = ref(true) // 列表的加载中
const list = ref<ProformaInvoiceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  invoiceDate: [],
  dueDate: [],
  kind: undefined,
  customerId: route.params.id,
  brandId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const brandList = ref([])
const getBrandList = async () => {
  brandList.value = await CommonApi.getBrandList()
}

const customer = ref({
  name: null,
  code: null,
  logo: null,
  address: null,
  contract: null,
  mobile: null,
  email: null,
  fax: null,
})
const getCustomerInfo = async () => {
  customer.value = await CommonApi.getCustomerDetail(route.params.id)
}

const customerList = ref([])
const getCompanyList = async () => {
  customerList.value = await CommonApi.getCustomerList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProformaInvoiceApi.getProformaInvoicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProformaInvoiceApi.deleteProformaInvoice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleConfirm = async (id: number) => {
  try {
    // 删除的二次确认
    await message.confirm('确认该订单后，不可编辑')
    // 发起删除
    await ProformaInvoiceApi.confirmProformaInvoice(id)
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProformaInvoiceApi.exportProformaInvoice(queryParams)
    download.excel(data, '形式发票.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getBrandList()
  getCustomerInfo()
  getCompanyList()
  getList()
})
</script>
